package com.wn.class30.tools;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @Description: SMSUtils 验证码发送方法
 * @author: jzy
 * @date: 2020/3/31 11:52 AM
 */
public class SMSUtils {
    private static final String ACCESSKEYID = "LTAI4FjztijbK1M5eE8Ynb3n";
    private static final String ACCESSKEYSECRET = "M28gDeb2duXQQHb1mf74VY93Pq1rif";

    /**
     * 我所申请的模板号，请对应更改模板号
     */
    public static final String VALIDATE_CODE = "SMS_186966945";


    /**
     * 发送短信
     *
     * @param phoneNumbers 接收短信的手机号码。国内短信：11位手机号码。
     */
    public static void sendShortMessage(String phoneNumbers,String authCode) {
        DefaultProfile profile = DefaultProfile.getProfile( "cn-hangzhou", ACCESSKEYID, ACCESSKEYSECRET );
        IAcsClient client = new DefaultAcsClient( profile );
        CommonRequest request = new CommonRequest();
        request.setMethod( MethodType.POST );
        request.setDomain( "dysmsapi.aliyuncs.com" );
        request.setVersion( "2017-05-25" );
        request.setAction( "SendSms" );
        request.putQueryParameter( "RegionId", "cn-hangzhou" );
        request.putQueryParameter( "PhoneNumbers", phoneNumbers );
        request.putQueryParameter( "SignName", "禄舍轩" );//短信签名名称。请在控制台签名管理页面签名名称一列查看。
        request.putQueryParameter( "TemplateCode", VALIDATE_CODE );
        request.putQueryParameter( "TemplateParam", "{\"code\":\"" + authCode + "\"}" ); //拼接成官网指定格式
        try {
            CommonResponse response = client.getCommonResponse( request );
            System.out.println( response.getData() ); //回显消息
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static String getVercode(){
        //拼接一个4位数验证码字符串
        StringBuffer stringBuffer = new StringBuffer();
        for(int i = 0;i < 4;i++){
            int num = RandomUtils.nextInt(10);
            stringBuffer.append(num);
        }
        return stringBuffer.toString();
    }
}